<?php

namespace app\admin\controller;

use think\Db;
use app\api\model\UserPunish;
use think\Session;

class Limit extends Common {
    // 禁言
    public function chat()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            if(in_array($search_type, ['id', 'phone'])){
                $where['t2.'.$search_type] = $search_name;
            }elseif(in_array($search_type, ['name'])){
                $where['t2.'.$search_type] = ['like', '%'.$search_name.'%'];
            }else{
                $where['t1.'.$search_type] = ['like', '%'.$search_name.'%'];
            }
        }

        $start = input('start');
        $end = input('end');

        if ($start && $end) {
            $where['t1.time'] = ['between', [strtotime($start), strtotime($end)]];
        } else if ($start && !$end) {
            $where['t1.time'] = ['egt', strtotime($start)];
        } else if (!$start && $end) {
            $where['t1.time'] = ['elt', strtotime($end)];
        }
        $this->assign('start', $start);
        $this->assign('end', $end);

        $data = Db::name('limit_chat')->alias('t1')->join('user t2','t1.user_id=t2.id')->where($where)->field('t1.*,t2.name')->order('t1.time desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $dataList = $data->all();

        $admin = Db::name('admin')->column('name','id');
        foreach ($dataList as &$value) {
            $value['admin_name'] = $admin[$value['admin_id']] ?? '';
        }
        
        $this->assign('data', $dataList);
        $page = $data->render();
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function chatAdd()
    {
        $post = input('post.');

        $user = Db::name('user')->find($post['user_id']);

        $time = time();
        $result = ['state' => 0, 'msg' => '操作失败'];
        if ($user) {
            UserPunish::inc($user['id'], 'limit_chat');
            $chat = Db::name('limit_chat')->where(['user_id'=>$post['user_id']])->find();
            if($chat){
                $result = ['state' => 0, 'msg' => '已设置限制'];
            }else{
                 Db::name('user')->where(['id'=>$post['user_id']])->update(['forbidden_speech'=>1]);
                $chatid = Db::name('limit_chat')->insertGetid(['user_id'=>$post['user_id'],'reason'=>$post['reason'], 'time' => $time, 'admin_id'=>$this->adminInfo['id']]);
                $result = ['state' => 1, 'msg' => '操作成功'];
            }
        }
        return $result;
    }

    public function chatDel()
    {
        $id = input('id');
        
        if (request()->isAjax()) {
             Db::name('user')->where(['id'=>$id])->update(['forbidden_speech'=>0]);
            Db::name('limit_chat')->where('user_id', $id)->delete();
            $data = ['state' => 1, 'msg' => '设置成功'];
        }
        return $data;
    }



    public function Device()
    {
        $where = ['delete_time'=>0];
        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            if(in_array($search_type, ['id', 'phone'])){
                $where[$search_type] = $search_name;
            }elseif(in_array($search_type, ['name'])){
                $where[$search_type] = ['like', '%'.$search_name.'%'];
            }else{
                $where[$search_type] = ['like', '%'.$search_name.'%'];
            }
        }

        $start = input('start');
        $end = input('end');

        if ($start && $end) {
            $where['time'] = ['between', [strtotime($start), strtotime($end)]];
        } else if ($start && !$end) {
            $where['time'] = ['egt', strtotime($start)];
        } else if (!$start && $end) {
            $where['time'] = ['elt', strtotime($end)];
        }
        $this->assign('start', $start);
        $this->assign('end', $end);

        $data = Db::name('limit_device')->where($where)->order('delete_time desc,id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);
        
        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function addDevice()
    {
        $post = input('post.');

        $time = time();
        $result = ['state' => 0, 'msg' => '操作失败'];
        $admininfo = Session::get('admin', 'mmjtAdmin');
        $admin_id = $admininfo['id'];
        $limit_device = Db::name('limit_device')->where(['device_id'=>$post['device_id'], 'delete_time'=>0])->find();
        if($limit_device){
            $result = ['state' => 0, 'msg' => '已存在列表'];
        }else{
            $limit_deviceid = Db::name('limit_device')->insertGetid(['device_id'=>$post['device_id'], 'reason'=>$post['reason'], 'create_time' => $time, 'admin_id'=>$admin_id, 'optsource'=>$post['optsource']]);
            $result = ['state' => 1, 'msg' => '操作成功'];
        }
        return $result;
    }
    
    public function unDevice()
    {
        $id = input('id');
        
        if (request()->isAjax()) {
            Db::name('limit_device')->where('id', $id)->update(['delete_time'=>time()]);
            $data = ['state' => 1, 'msg' => '设置成功'];
        }
        return $data;
    }


}